<?php
class WbPageNewsController extends  DomController {

	public function page( $pageId ) {

		$item = WbPageNews::load( $pageId );

		$this->loadController('Wb')->_updatePage( $item );
		
		if( $_GET['delete'] && $newsEntry = WbPageNewsEntry::load($_GET['delete']) ) {
			$newsEntry->delete();
		}

		$post = DomFgPost::getInstance();
		if( $post->save ) {
			
			$item->title = $post->title;
			$item->contents = $post->contents;
			
			if( $item->save() ) {
				$message = array('type' => 'ok','colspan' => 2,'message' => 'Salvestatud',);
			} else {
				$message = array('message' => 'Tekkinud on viga!','type' => 'ok','colspan' => 2,'description' => implode('<br>',$item->getValidationReport()->messages),);
			}
		}
		
		$tabPanel = array(
			'type' => 'tabPanel',
			'action' => $this->getUrl('page/'.$pageId),
			'headerTabs' => true,
			'items' => array($message),
			'buttons' => array(
				array(
					'title' => 'Salvesta',
					'name' => 'save'
				),
			),
		);
		
		// -------------------------------------------
		
		
		$overallTab = array(
			'title' => 'Üldine',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array( 150 ),
			),
			'items' => array(
				$this->loadController('Wb')->_getPageItems( $item ),
			),
		);
		$tabPanel['items'][] = &$overallTab;
		
		// -------------------------------------------
		
		$newsTab = array(
			'title' => 'Uudised',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 1,
				'columnWidths' => array( 150 ),
			),
			'items' => array(
				array(
					'type' => 'button',
					'title' => 'Lisa uus uudis',
					'link' => $this->getUrl('newsEntry/'.$pageId),
				),
				array(
					'type' => 'grid',
					'source' => new DomArDataSource( $item->entries->query()->orderBy('t.datetime DESC'), 40 ),
					'fields' => array(
						array('key' => 'title', 'title' => 'Pealkiri'),
						array('type' => 'gridButton', 'value' => 'Muuda', 'link' => $this->getUrl('newsEntry/'.$pageId.'/'), 'title' => 'Valikud','colspan' => 2, 'width'=>60),
						array('type' => 'gridButton', 'value' => 'Kustuta', 'link' => $this->getUrl('page/'.$pageId.'&delete='), 'confirmMessage' => 'Are you sure?', 'width'=>60 ),
					),
				),
			),
		);
		$tabPanel['items'][] = &$newsTab;

		// -------------------------------------------
		
		$contentTab = array(
			'title' => 'Sisu',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array(),
			),
			'items' => array(
				array(
					'type'  => 'textField',	
					'name' => 'title',
					'value'  => $item->title,
					'title' => 'Pealkiri',
					'style' => 'width:500px',
				),
				array(
					'type' => 'htmlEditor',
					'value' => $item->contents,
					'name' => 'contents',
					'style' => 'height:400px;',
					'colspan' => 2,
				),
			),
		);
		$tabPanel['items'][] = &$contentTab;
		
		// -------------------------------------------
		
		$tabPanel['items'][] = $this->loadController('Wb')->_getPagePanels( $item );
		
		// -------------------------------------------
		$this->view->contents = DomFg::render( $tabPanel );
		$this->loadController('Wb')->_renderSiteTree( $item->getRoot(), $item );
		
	}

	public function newsEntry( $pageId, $id = NULL) {
		$new = $id == 0;
		$item = $new ? new WbPageNewsEntry() : WbPageNewsEntry::load($id);
		if( $new == true ) {
			$item->parent = WbPageNews::load( $pageId );
		}
		
		
		$post = DomFgPost::getInstance();
		
		if( $post->deleteImage ) {
			$item->image->unlink();
			$item->icon->unlink();
			$item->save();
		}
		
		if( $post->save ) {
			
			$item->title = $post->title;
			$item->datetime = $post->datetime;
			$item->image->addUploaded( $post->image[0], $post->image[1] );
			$item->contents = $post->contents;
			
			if( $item->save() ) {
				$id = $item->id;
				$message = array('type' => 'ok','colspan' => 4,'message' => 'Salvestatud',);
			} else {
				$message = array('message' => 'Tekkinud on viga!','type' => 'ok','colspan' => 4,'description' => implode('<br>',$item->getValidationReport()->messages),);
			}
		}
				
		$panel = array(
			'title' => 'Uudis',
			'layout' => 'table',
			'action' => $this->getUrl('newsEntry/'.$pageId.'/'.$id),
			'layoutConfig' => array(
				'columns' => 4,
				'columnWidths' => array( 100, 150,250 ),
			),
			'items' => array(
				$message ,
				array(
					'type' => 'image',
					'src' => $item->icon->getUrl(),
					'style' => 'width:100px; height:100px;',
					'rowspan' => 5,
				),
				array(
					'type' => 'textField',
					'title' => 'Pealkiri',
					'value' => $item->title,
					'name' => 'title',
					'colspan' => 2,
				),
				array(
					'type' => 'dateTime',
					'title' => 'Kuupäev',
					'value' => $item->datetime,
					'name' => 'datetime',
				'colspan' => 2,
				),
				array(
					'type' => 'file',
					'title' => 'Pilt',
					'name' => 'image',
				),
				array(
					'type' => 'button',
					'title' => 'Kustuta pilt',
					'name' => 'deleteImage',
					'value' => '1'
				),
				array(
					'type' => 'htmlEditor',
					'title' => 'Sisu',
					'value' => $item->contents,
					'name' => 'contents',
					'colspan' => 2,
				),
			),
			'buttons' => array(
				array(
					'title' => 'Salvesta',
					'name' => 'save'
				),
				array(
					'title' => 'Tagasi',
					'link' => $this->getUrl('page/'.$pageId),
				),
			),
		);

		$this->view->contents = DomFg::render( $panel );
		$this->loadController('Wb')->_renderSiteTree( $item->parent->getRoot(), $item->parent );
	}
	
}